﻿// ===============================================================================
// Trace.cs
// EOS Framework
// ===============================================================================
// Copyright (c) EOS Development Group. All rights reserved.
// ===============================================================================

namespace EOS.Framework.Tools.Aspects
{
    using System;
    using PostSharp.Laos;

    /// <summary>
    /// Aspect for tracing method calls.
    /// </summary>
    [AttributeUsage(AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property)]
    public sealed class Trace : OnMethodBoundaryAspect
    {
        /// <summary>
        /// Traces, that the method was entered.
        /// </summary>
        /// <param name="eventArgs">Event arguments specifying which method
        /// is being executed, which are its arguments, and how should the execution continue
        /// after the execution of <see cref="M:PostSharp.Laos.IOnMethodBoundaryAspect.OnEntry(PostSharp.Laos.MethodExecutionEventArgs)"/>.</param>
        /// <remarks>
        /// If the aspect is applied to a constructor, the current method is invoked
        /// after the <b>this</b> pointer has been initialized, that is, after
        /// the base constructor has been called.
        /// </remarks>
        public override void OnEntry(MethodExecutionEventArgs eventArgs)
        {
            System.Diagnostics.Trace.WriteLine("Method {0} entered", eventArgs.Method.Name);

            base.OnEntry(eventArgs);
        }

        /// <summary>
        /// Traces, that the method was leaved.
        /// </summary>
        /// <param name="eventArgs">Event arguments specifying which method
        /// is being executed and which are its arguments.</param>
        public override void OnExit(MethodExecutionEventArgs eventArgs)
        {
            System.Diagnostics.Trace.WriteLine("Method {0} leaved", eventArgs.Method.Name);

            base.OnExit(eventArgs);
        }
    }
}
